﻿using System.Collections.Generic;
using System.Linq;
using mitasoft.Web.DAL.Model;
using MySite2.Areas.RedZone.Helpers;
using MySite2.ViewModel;

namespace MySite2.ViewModel
{
    public class BooksViewModel : BaseViewModel
    {
        public PaginatedListBook PaginatedBookList { get; private set; }

        public BooksViewModel()
        {

        }

        public ICollection<Book> GetBooks()
        {
            return _repository.Books.ToList();
        }

        public ICollection<Book> GetTop3Books()
        {
            return _repository.Books
                .Take(4)
                .OrderByDescending(b => b.AddDate)
                .ToList();
        }


        public Book GetBook(int id)
        {
            return _repository.GetBook(id);
        }

        public void GetBooks(int pageNumber)
        {
            int pageSize = 4;
            PaginatedBookList = new PaginatedListBook(_repository.Books, pageNumber, pageSize);
        }

        public void AddBook(Book book)
        {
            _repository.AddBook(book);
            _repository.SaveChanges();
        }

        public void EditBook(Book book)
        {
            _repository.EditBook(book);
            _repository.SaveChanges();
        }

        public void RemoveBook(int id)
        {
            _repository.DeleteBook(id);
            _repository.SaveChanges();
        }

    }
}